//
//  JYShopCell.m
//  JYFarm
//
//  Created by 花花 on 2017/10/16.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYShopCell.h"
#import "JYFontColorTool.h"
@implementation JYShopCell
- (void)awakeFromNib {
    [super awakeFromNib];
    self.minBtn.layer.borderWidth =1.0;
    self.minBtn.layer.borderColor = [UIColor colorWithHexString:@"62C6B2"].CGColor;
    self.minBtn.layer.cornerRadius = self.minBtn.frame.size.width * 0.5;
    
    
    
    self.maxBtn.layer.borderWidth =1.0;
    self.maxBtn.layer.borderColor = [UIColor colorWithHexString:@"62C6B2"].CGColor;
    self.maxBtn.layer.cornerRadius = self.maxBtn.frame.size.width * 0.5;
    self.maxBtn.layer.masksToBounds = YES;
    
    
    self.bordView.layer.borderWidth = 1;
    self.bordView.layer.borderColor = [UIColor colorWithHexString:@"F2F2F2"].CGColor;
}

- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index {
    [super insertSubview:view atIndex:index];
    
    if ([view isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
        view.top = 10;
        view.height = self.height - 10;
        
        for (UIButton *btn in view.subviews) {
            
            if ([btn isKindOfClass:[UIButton class]]) {
                
                [btn setBackgroundColor:kf2Color];
                
                [btn setTitle:nil forState:UIControlStateNormal];
                
                [btn setImage:[UIImage imageNamed:@"list_deleting"] forState:UIControlStateNormal];
                
                [btn setTintColor:[UIColor whiteColor]];
            }
        }
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    
}

- (void)configureShopcartCountWithProductCount:(NSInteger)productCount productStock:(NSInteger)productStock{
    
    if (productCount == 1) {
        self.minBtn.enabled = NO;
        self.maxBtn.enabled = YES;
    }else if (productCount == productStock){
        
        self.minBtn.enabled = YES;
        self.maxBtn.enabled = NO;
    }else{
        
        self.minBtn.enabled = YES;
        self.maxBtn.enabled = YES;
    }
}

//减
- (IBAction)minOnClick {
    
    NSInteger count = self.count.text.integerValue;
    if (self.shopcartCountEditBlock) {
        self.shopcartCountEditBlock(--count);
    }
    
}
//加
- (IBAction)addOnClick {
    
    NSInteger count = self.count.text.integerValue;
    if (self.shopcartCountEditBlock) {
        
        self.shopcartCountEditBlock(++count);
    }
    
}



//是否选择
- (IBAction)selOnClick:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    if (self.selectBtnClick) {
        self.selectBtnClick(sender.selected);
    }
}

- (void)setModel:(JYShopModel *)model{

    _model = model;

    self.nameLabel.text = model.goodsName;
    
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:model.goodsPictureUrl] placeholderImage:defaultFarmImage];
    self.priceLabel.text = [NSString stringWithFormat:@"￥%.2f",[model.goodsPrice doubleValue]];
    self.unitLabel.text = model.unit;
    self.oldPriceLabel.text = [NSString stringWithFormat:@"￥%.2f",[model.originalprice doubleValue]];

    self.count.text = [NSString stringWithFormat:@"%ld",(long)model.goodsAmount];
    
    
    if (model.selected) {
        self.selBtn.selected = YES;
    }else{
        self.selBtn.selected = NO;
    }

    
}


@end
